# @author: uuz
# encoding = utf-8
from flask import Blueprint, jsonify
from flask_restful import Api, Resource, reqparse, abort
from sqlalchemy import or_

from ext import db
from models import Hotel, QandA

hotel_bp = Blueprint("hotel", __name__, url_prefix='/api/hotel')
api = Api(hotel_bp)


class AddHotelView(Resource):
    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('name', type=str, help='请输入正确名称', required=True)
        self.parser.add_argument('type', type=str, help='请输入正确类型', required=True)
        self.parser.add_argument('point', type=float, help='请输入正确评分', required=True)
        self.parser.add_argument('fans', type=int, required=True)
        self.parser.add_argument('comment_amount', type=int, required=True)
        self.parser.add_argument('subtypes', type=str, required=True)
        self.parser.add_argument('detail', type=str, required=True)
        self.parser.add_argument('location', type=str, required=True)
        self.parser.add_argument('traffic', type=str, required=True)
        self.parser.add_argument('tel', type=str, required=True)

    def post(self):
        args = self.parser.parse_args()
        hotel_name = args.get('name')
        hotel_type = args.get('type')
        hotel_point = args.get('point')
        hotel_fans = args.get('fans')
        hotel_comment = args.get('comment_amount')
        hotel_subtypes = args.get('subtypes')
        hotel_detail = args.get('detail')
        hotel_location = args.get('location')
        hotel_traffic = args.get('traffic')
        hotel_tel = args.get('tel')

        hotel = Hotel(name=hotel_name,
                      type=hotel_type,
                      point=hotel_point,
                      fans=hotel_fans,
                      comment_amount=hotel_comment,
                      subtypes=hotel_subtypes,
                      detail=hotel_detail,
                      location=hotel_location,
                      traffic=hotel_traffic,
                      tel=hotel_tel)

        db.session.add(hotel)
        db.session.commit()
        return jsonify({"result": "OK"})


api.add_resource(AddHotelView, '/Hotel-Add/')


class DeleteHotelView(Resource):
    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('hotel_id', type=int, required=True)

    def post(self):
        args = self.parser.parse_args()
        hotel = Hotel.query.filter(Hotel.id == args.get('hotel_id')).first()
        if hotel:
            db.session.delete(hotel)
            db.session.commit()
            return jsonify({"result": "删除成功"})
        else:
            abort(400, message="酒店不存在")


api.add_resource(DeleteHotelView, '/Hotel-Delete/')


class AddQuestionView(Resource):
    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('hotel_id', type=int, required=True)
        self.parser.add_argument('question', type=str, required=True)

    def post(self):
        args = self.parser.parse_args()
        hotel_id = args.get('hotel_id')
        if Hotel.query.filter(Hotel.id == hotel_id).first():
            question = args.get('question')

            qaa = QandA(hotel_id=hotel_id,
                        question=question)
            db.session.add(qaa)
            db.session.commit()
            return jsonify({"result": "提问成功"})
        else:
            abort(400, messaeg="酒店id错误")


api.add_resource(AddQuestionView, '/Question-Add/')


class AddAnswerView(Resource):
    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('question_id', type=int, required=True)
        self.parser.add_argument('answer', type=str, required=True)

    def post(self):
        args = self.parser.parse_args()
        question_id = args.get('question_id')
        qaa = QandA.query.filter(QandA.id == question_id).first()
        if qaa:
            qaa.answer = args.get('answer')
            db.session.commit()
            return jsonify({"result": "回答成功"})
        else:
            abort(400, messaeg="问题id错误")


api.add_resource(AddAnswerView, '/Answer-Add/')


class DeleteQAView(Resource):
    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('qaa_id', type=int, required=True)

    def post(self):
        args = self.parser.parse_args()
        qaa = QandA.query.filter(QandA.id == args.get('qaa_id')).first()
        if qaa:
            db.session.delete(qaa)
            db.session.commit()
            return jsonify({"result": "删除成功"})
        else:
            abort(400, message="问题不存在")


api.add_resource(DeleteQAView, '/QandA-Delete/')


class QAListView(Resource):
    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('hotel_id', type=int, required=True)

    def get(self):
        hotel_id = self.parser.parse_args().get('hotel_id')
        qas = Hotel.query.filter(Hotel.id == hotel_id).first().qas

        if qas:
            lst = []
            for qa in qas:
                lst.append({
                    "id": qa.id,
                    "hotel_id": qa.hotel_id,
                    "question": qa.question,
                    "answer": qa.answer
                })
            return lst
        else:
            abort(400, message="该酒店暂无问答")


api.add_resource(QAListView, '/QueryQAListByHotelId/')


class HotelListView(Resource):

    def get(self):
        hotels = Hotel.query.all()
        lst = []
        for hotel in hotels:
            lst.append({
                "id": hotel.id,
                "name": hotel.name,
                "type": hotel.type,
                "point": hotel.point,
                "fans": hotel.fans,
                "comments": hotel.comment_amount,
                "subtypes": hotel.subtypes,
                "detail": hotel.detail,
                "location": hotel.location,
                "traffic": hotel.traffic,
                "tel": hotel.tel
            })
        return lst


api.add_resource(HotelListView, '/Hotel-List/')


class QueryHotelById(Resource):
    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('hotel_id', type=int, required=True)

    def get(self):
        hotel_id = self.parser.parse_args().get('hotel_id')
        hotel = Hotel.query.filter(Hotel.id == hotel_id).first()
        if hotel:
            return {
                "id": hotel.id,
                "name": hotel.name,
                "type": hotel.type,
                "point": hotel.point,
                "fans": hotel.fans,
                "comments": hotel.comment_amount,
                "subtypes": hotel.subtypes,
                "detail": hotel.detail,
                "location": hotel.location,
                "traffic": hotel.traffic,
                "tel": hotel.tel
            }
        else:
            abort(400, message="酒店编号不存在")


api.add_resource(QueryHotelById, '/QueryById/')


class QueryHotelByKeyword(Resource):
    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('keyword', type=str, required=True)

    def get(self):
        keyword = self.parser.parse_args().get('keyword')
        hotels = Hotel.query.filter(
            or_(
                Hotel.name.like("%" + keyword + "%"),
                Hotel.type.like("%" + keyword + "%"),
                Hotel.detail.like("%" + keyword + "%"),
                Hotel.location.like("%" + keyword + "%"),
                Hotel.traffic.like("%" + keyword + "%")
            )
        ).all()

        if hotels:
            lst = []
            for hotel in hotels:
                lst.append({
                    "id": hotel.id,
                    "name": hotel.name,
                    "type": hotel.type,
                    "point": hotel.point,
                    "fans": hotel.fans,
                    "comments": hotel.comment_amount,
                    "subtypes": hotel.subtypes,
                    "detail": hotel.detail,
                    "location": hotel.location,
                    "traffic": hotel.traffic,
                    "tel": hotel.tel
                })
            return lst
        else:
            abort(400, message="未找到相关酒店")


api.add_resource(QueryHotelByKeyword, '/QueryByKeyword/')
